
#include "stdafx.h"

TEST(bindTest, bind_in_inout)
{
    SOCKET sock = INVALID_SOCKET;
    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(27015);
    int len = 11;
    
    Header header(GetCurrentThreadId(), "bind");
    bind_in_inout bindIN(sock, (SOCKADDR*)&addr, len);
    bind_in_inout bindOUT;

    std::vector<char> buffer;
    Serialize(header, bindIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(bindOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (bindIN.s_, bindOUT.s_);
    if (bindIN.addr_ && bindOUT.addr_)
    {
        EXPECT_EQ (bindIN.addr_->sa_family, bindOUT.addr_->sa_family);
        int cmp = strncmp (bindIN.addr_->sa_data, bindOUT.addr_->sa_data, 14);
        EXPECT_TRUE (!cmp);
    }
    else {
        EXPECT_FALSE(bindIN.addr_);
        EXPECT_FALSE(bindOUT.addr_);
    }
    EXPECT_EQ (bindIN.addrlen_, bindOUT.addrlen_);
}

TEST(bindTest, bind_in_inout_NULL_parameters)
{
    SOCKET sock = INVALID_SOCKET;
    int len = 11;
    
    Header header(GetCurrentThreadId(), "bind");
    bind_in_inout bindIN(sock, NULL, len);
    bind_in_inout bindOUT;

    std::vector<char> buffer;
    Serialize(header, bindIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(bindOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (bindIN.s_, bindOUT.s_);
    if (bindIN.addr_ && bindOUT.addr_)
    {
        EXPECT_EQ (bindIN.addr_->sa_family, bindOUT.addr_->sa_family);
        int cmp = strncmp (bindIN.addr_->sa_data, bindOUT.addr_->sa_data, 14);
        EXPECT_TRUE (!cmp);
    }
    else {
        EXPECT_FALSE(bindIN.addr_);
        EXPECT_FALSE(bindOUT.addr_);
    }
    EXPECT_EQ (bindIN.addrlen_, bindOUT.addrlen_);
}

TEST(bindTest, bind_out_inout)
{
    SOCKET sock = INVALID_SOCKET;
    
    Header header(GetCurrentThreadId(), "bind");
    bind_out_inout bindIN(7);
    bind_out_inout bindOUT;

    std::vector<char> buffer;
    Serialize(header, bindIN, buffer);
        
    Deserialize deserialize(buffer);
    deserialize(bindOUT);

    EXPECT_EQ (header.TID_, deserialize.get_header().TID_);
    EXPECT_EQ (header.func_name_, deserialize.get_header().func_name_);

    EXPECT_EQ (bindIN.wsa_last_error_, bindOUT.wsa_last_error_);
}

//TEST(bindTest, bind_out_inout_NULL_parameters)
//{
//    
//}